package com.ruoyi.common.utils.pdf;

import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;

/**
 * Excel相关处理
 * 
 * @author ruoyi
 */
public class PdfUtil
{
    /**
     * 添加页眉
     * @param page
     * @param font 页眉字体
     * @param headerText 页眉文字
     */
    public  static void addHeader(PdfPage page, PdfFont font,String headerText) {
        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), page.getDocument());
        // 获取页面的矩形区域
        Rectangle pageRectangle = page.getPageSize();
        try (Canvas canvas = new Canvas(pdfCanvas, pageRectangle)) {
            // 设置页眉文字
            Paragraph headerParagraph = new Paragraph(headerText).setFont(font).setFontSize(8).setFontColor(new DeviceRgb(6, 6, 6));

            // 设置页眉位置，这里将其放在页面顶部居中
            float x = pageRectangle.getWidth();
            float y = pageRectangle.getHeight() - 30;
            canvas.showTextAligned(headerParagraph, x, y, TextAlignment.LEFT);
        }
    }

    /**
     * 添加页脚
     * @param page
     * @param font
     * @param footerText 页脚内容
     * @return
     */
    public static void addFooter(PdfPage page, PdfFont font, String footerText) {
        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), page.getDocument());
        Rectangle pageRectangle = page.getPageSize();
        try (Canvas canvas = new Canvas(pdfCanvas, pageRectangle)) {
            // 设置页脚文字
            Color lightColor = new DeviceRgb(128, 128, 128);
            Paragraph footerParagraph = new Paragraph(footerText).setFont(font).setFontSize(10).setFontColor(lightColor);

            // 设置页脚位置，这里将其放在页面底部居中
            float x = pageRectangle.getWidth() / 2;
            float y = 20;
            canvas.showTextAligned(footerParagraph, x, y, TextAlignment.CENTER);
        }
    }
}
